package ReflectDemo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

/*
       反射可以越过泛型检查。
       练习 : 如何往 ArrayList<Integer>集合中添加字符串数据?   ---通过反射实现
 */
public class ReflectDemo8 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(10);
        Class<? extends ArrayList> aClass = arrayList.getClass();
        Method add = aClass.getDeclaredMethod("add", Object.class);
        add.invoke(arrayList,"hello");
        add.invoke(arrayList,"world");
        add.invoke(arrayList,"java");

        System.out.println(arrayList);

    }
}
